package com.tfswx.jkgl.core.yzfs;

import cn.hutool.extra.spring.SpringUtil;
import com.tfswx.jkgl.core.bean.Csdy;
import com.tfswx.jkgl.core.yzfs.base.YzfsBase;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ricky
 * @date 2021/12/5
 */
public class YzfsUtil {
    private static final Map<String, YzfsBase> yzfsBaseMap = SpringUtil.getBeansOfType(YzfsBase.class);

    private static YzfsBase getYzfs(String yzlx) {
        if (StringUtils.isBlank(yzlx)) {
            return null;
        }
        String s = String.valueOf(yzlx.charAt(0));
        yzlx = yzlx.replaceFirst(s, s.toLowerCase());
        YzfsBase yzfsBase = yzfsBaseMap.get(yzlx);
        if (yzfsBase == null) {
            throw new RuntimeException("没有该验证方式");
        }
        return yzfsBase;
    }

    public static List<String> valid(Csdy csdy, Object inputDTO) {
        List<String> errList = new ArrayList<>();
        String cslx = csdy.getCslx();
        if ("List".equals(cslx)) {
            // 是List
            errList.addAll(yzList(csdy, inputDTO));
            errList.addAll(yzOther(csdy, inputDTO));
        } else if ("Map".equals(cslx)) {
            // 是Map
            errList.addAll(yzMap(csdy, inputDTO));
            errList.addAll(yzOther(csdy, inputDTO));
        } else {
            errList.addAll(yzOther(csdy, inputDTO));
        }
        return errList;
    }

    public static List<String> yzOther(Csdy csdy, Object inputDTO) {
        List<Csdy.Yzfs> jmfs = csdy.getYzfs();
        if (jmfs == null) {
            return new ArrayList<>();
        }
        List<String> errList = new ArrayList<>();
        for (Csdy.Yzfs jmf : jmfs) {
            YzfsBase yzfsBase = getYzfs(jmf.getYzlx());
            if (yzfsBase == null) {
                continue;
            }
            boolean valid = yzfsBase.isValid(inputDTO, jmf.getYzz());
            if (!valid) {
                String message = jmf.getTsxx();
                if (StringUtils.isBlank(message)) {
                    message = csdy.getCsbm() + "-" + yzfsBase.getMessage();
                }
                errList.add(message);
            }
        }
        return errList;
    }

    public static List<String> yzMap(Csdy csdy, Object inputDTO) {
        List<String> errList = new ArrayList<>();
        List<Csdy> csdy1 = csdy.getCsdy();
        String csbm = csdy.getCsbm();
        if (!(inputDTO instanceof Map)) {
            throw new RuntimeException("入（出）参" + (csbm != null ? ("【" + csbm + "】") : "") + "不是对象，请检查配置！");
        }
        for (Csdy child : csdy1) {
            Map<String, Object> map = (Map<String, Object>) inputDTO;
            List<String> valid = valid(child, map.get(child.getCsbm()));
            errList.addAll(valid);
        }
        return errList;
    }

    public static List<String> yzList(Csdy csdy, Object inputDTO) {
        List<String> errList = new ArrayList<>();
        String csbm = csdy.getCsbm();
        if (!(inputDTO instanceof List)) {
            throw new RuntimeException("入（出）参" + (csbm != null ? ("【" + csbm + "】") : "") + "不是列表，请检查配置！");
        }
        List<Object> value = (List<Object>) inputDTO;
        String jhlx = csdy.getJhlx();
        for (Object o : value) {
            if ("Map".equals(jhlx)) {
                errList.addAll(yzMap(csdy, o));
                errList.addAll(yzOther(csdy, o));
            } else {
                if (o instanceof Map) {
                    throw new RuntimeException("参数[" + csbm + "]集合类型不匹配,期望:字符串,获得:对象");
                }
                errList.addAll(yzOther(csdy, o));
            }
        }
        return errList;
    }

    public static Map<String, YzfsBase> getYzfsMap() {
        return yzfsBaseMap;
    }
}
